package src.bean;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCIteratorBinding;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

import oracle.jbo.Key;


public class DepartmentBean {
    private String mode = "CONSULTATION";

    public DepartmentBean() {
    }


    public void setMode(String mode) {
        this.mode = mode;
    }

    public String getMode() {
        return mode;
    }

    /*
     * Methods
     */
    public void updateDepartment() {
        this.mode = "EDITION";
    }

    public void cancelCreateOrEdit() {
        this.mode = "CONSULTATION";
    }


    public void createDepartment() {
        this.mode = "CREATION";
    }


    public BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    public String onRollback() {
        BindingContainer bindings = getBindings();
        //get rowKey for parent and child
        DCIteratorBinding parentIter = (DCIteratorBinding) bindings.get("DepartmentsView1Iterator");
        DCIteratorBinding childIter = (DCIteratorBinding) bindings.get("DepartmentsView1Iterator");
        Key parentKey = parentIter.getCurrentRow().getKey();
        Key childKey = childIter.getCurrentRow().getKey();
        // perform rollback operation
        OperationBinding operationBinding = bindings.getOperationBinding("Rollback");
        //Object result = operationBinding.execute();
        if (!operationBinding.getErrors().isEmpty()) {
            return null;
        }
        //user row key back to preserve row selection
        parentIter.setCurrentRowWithKey(parentKey.toStringFormat(true));
        childIter.setCurrentRowWithKey(childKey.toStringFormat(true));
        return null;
    }
}
